using System;
using System.Windows.Forms;
using FastColoredTextBoxNS;

namespace SS.Ynote.Classic.Utils
{
    public enum ProtectionType
    {
        Encrypt,
        Decrypt
    }
    /// <summary>
    /// Utility Dialog
    /// </summary>
    public partial class EncryptionDialog : Form
    {
        private ProtectionType pt { get; set; }
        public string File { get; set; }
        private Main MyParent { get; set; }
        /// <summary>
        /// Default Construtor
        /// </summary>
        /// <param name="file"></param>
        public EncryptionDialog(string file, Main parent, ProtectionType type)
        {
            InitializeComponent();
            if (type == ProtectionType.Encrypt)
                this.Text += " " + System.IO.Path.GetFileName(file);
            else
            {
                this.Text = "Decrypt File " + System.IO.Path.GetFileName(file);
                button1.Text = "Decrypt";
            }
            File = file;
            MyParent = parent;
            pt = type;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pt == ProtectionType.Encrypt)
            {
                System.IO.File.WriteAllBytes(File, Encryption.Encrypt(System.IO.File.ReadAllBytes(File), textBox1.Text));
                MyParent.ActiveEditor.Close();
            }
            else if (pt == ProtectionType.Decrypt)
            {
                System.IO.File.WriteAllBytes(File, Encryption.Decrypt(System.IO.File.ReadAllBytes(File), textBox1.Text));
                MyParent.ActiveEditor.Close();
                MyParent.OpenFile(File, 0);
            }
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
